<?php

namespace App\Http\Controllers\Post;

use App\Http\Requests\getStatusArticle;
use App\Http\Requests\updateArticle;
use App\Http\Requests\getStatusArticleCount;
use App\Http\Requests\Drafts;
use App\Post;
use App\Tag;
use App\Post_tag;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
    //获取用户的所有文章
    public function  getAllArticle(Request $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $data = Post::select("id", "title", "created_at", "view", "like", "status")->where('user_id', $user_id)
            ->orderBy('created_at', 'desc')->paginate(10);
        if (sizeof($data) != 0) {
            return response()->json(['code' => 200, 'msg' => '成功', 'data' => $data]);
        } else {
            $data = "暂无内容";
            return response()->json(['code' => 100, 'msg' => '失败', 'data' => $data]);
        }
    }
    //查看用户的某个状态下的所有文章
    public function getStatusArticle(getStatusArticle $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $status = $request->status;
        if ($status != 3) {
            $data = Post::select("id", "title", "created_at", "view", "like", "status")
                ->where('user_id', $user_id)->where('status', $status)
                ->orderBy('created_at', 'desc')->paginate(10);
        } else {
            $data = Post::select("id", "title", "created_at")
                ->where('user_id', $user_id)->where('status', $status)
                ->orderBy('created_at', 'desc')->paginate(10);
        }
        return response()->json(['code' => 200, 'msg' => '成功', 'data' => $data]);
    }
    //获取用户的所有文章的总数Request $request
    public function getAllArticleCount()
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $num = Post::where('user_id', $user_id)->count();
        return response()->json(['code' => 200, 'msg' => '成功', 'data' => $num]);
    }
    //获取用户的某个状态的文章的数量
    public function getStatusArticleCount(getStatusArticleCount $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $status = $request->status;
        $num = Post::where('user_id', $user_id)
            ->where('status', $status)->count();
        return response()->json(['code' => 200, 'msg' => '成功', 'data' => $num]);
    }

    //查看自己的文章
    public function getOwnArticle(Request $request)
    {
        // $user_id = Auth::id();
        $user_id = $request->user_id;
        $post_id = $request->post_id;
        $data_post = Post::where('id', $post_id)->where('user_id', $user_id)
            ->first();
        if (empty($data_post)) {
            $data = "文章不存在";
            return response()->json(['code' => 100, 'msg' => '失败', 'data' => $data]);
        } else {
            $data_tag = Post_tag::where('post_id', $post_id)->get();
            $tag_id = [];
            for ($i = 0; $i < sizeof($data_tag); $i++) {
                $tag_id[$i] = $data_tag[$i]->tag_id;
            }
            $data = [
                'id'        =>  $post_id,
                'title'     =>  $data_post->title,
                'column_id' =>  $data_post->column_id,
                'tag_id'    =>  $tag_id,
                'body'      =>  $data_post->body,
                'mg_path'   =>  $data_post->mg_path
            ];
            return response()->json(['code' => 200, 'msg' => '成功', 'data' => $data]);
        }
    }

    //删除草稿
    public function DeleteDrafts(Request $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $post_id = $request->post_id;
        $result = Post::where('id', $post_id)->where('user_id', $user_id)
            ->where('status', 3)->delete();
        if ($result) {
            return response()->json(['code' => 200, 'msg' => '成功']);
        } else {
            return response()->json(['code' => 100, 'msg' => '失败']);
        }
    }

    //修改并发布用户的某篇文章(需要将状态改为1)
    public function UpdateArticle(updateArticle $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $post_id = $request->id;
        $tag_id = $request->tag_id;
        $tag_n = true;
        $tag_o = true;
        $column_id = Tag::select('column_id')->where('id', $tag_id[0])->first();
        $column_id = $column_id->column_id;
        $result_posts = Post::where('id', $post_id)->where('user_id', $user_id)
            ->update([
                'title'     => $request->title,
                'column_id' => $column_id,
                'body'      => $request->body,
                'img_path'  => $request->img_path,
                'status'    => 1 //1为待审核
            ]);
        $tag_old = Post_tag::select('tag_id')->where('post_id', $post_id)->get();
        //删除修改时不需要的老标签
        for ($i = 0; $i < sizeof($tag_old); $i++) {
            $vis = true;
            for ($j = 0; $j < sizeof($tag_id); $j++) {
                if ($tag_old[$i]->tag_id == $tag_id[$j]) {
                    $vis = false;
                }
            }
            if ($vis) {
                $result_old = Post_tag::where('post_id', $post_id)
                    ->where('tag_id', $tag_old[$i]->tag_id)->delete();
                if (!$result_old) {
                    $tag_o = false;
                }
            }
        }
        //添加修改前没有的新标签
        for ($i = 0; $i < sizeof($tag_id); $i++) {
            $vis = true;
            for ($j = 0; $j < sizeof($tag_old); $j++) {
                if ($tag_id[$i]  == $tag_old[$j]->tag_id) {
                    $vis = false;
                }
            }
            if ($vis) {
                $model =  new Post_tag();
                $model->post_id = $post_id;
                $model->tag_id = $tag_id[$i];
                $result_new = $model->save();
                if (!$result_new) {
                    $tag_n = false;
                }
            }
        }
        if ($result_posts && $tag_n && $tag_o) {
            return response()->json(['code' => 200, 'msg' => '成功']);
        } else {
            return response()->json(['code' => 100, 'msg' => '失败']);
        }
    }

    //修改并存为草稿
    public function Drafts(Drafts $request)
    {
        $user_id = Auth::id();
        //$user_id=$request->user_id;
        $post_id = $request->id;
        $tag_id = $request->tag_id;
        $tag_n = true;
        $tag_o = true;
        $column_id = Tag::select('column_id')->where('id', $tag_id[0])->first();
        $column_id = $column_id->column_id;
        $result_posts = Post::where('id', $post_id)->where('user_id', $user_id)
            ->update([
                'title'     => $request->title,
                'column_id' => $column_id,
                'body'      => $request->body,
                'img_path'  => $request->img_path,
                'status'    => 3 //3为草稿
            ]);
        $tag_old = Post_tag::select('tag_id')->where('post_id', $post_id)->get();
        //删除修改时不需要的老标签
        for ($i = 0; $i < sizeof($tag_old); $i++) {
            $vis = true;
            for ($j = 0; $j < sizeof($tag_id); $j++) {
                if ($tag_old[$i]->tag_id == $tag_id[$j]) {
                    $vis = false;
                }
            }
            if ($vis) {
                $result_old = Post_tag::where('post_id', $post_id)
                    ->where('tag_id', $tag_old[$i]->tag_id)->delete();
                if (!$result_old) {
                    $tag_o = false;
                }
            }
        }
        //添加修改前没有的新标签
        for ($i = 0; $i < sizeof($tag_id); $i++) {
            $vis = true;
            for ($j = 0; $j < sizeof($tag_old); $j++) {
                if ($tag_id[$i]  == $tag_old[$j]->tag_id) {
                    $vis = false;
                }
            }
            if ($vis) {
                $model =  new Post_tag();
                $model->post_id = $post_id;
                $model->tag_id = $tag_id[$i];
                $result_new = $model->save();
                if (!$result_new) {
                    $tag_n = false;
                }
            }
        }
        if ($result_posts && $tag_n && $tag_o) {
            return response()->json(['code' => 200, 'msg' => '成功']);
        } else {
            return response()->json(['code' => 100, 'msg' => '失败']);
        }
    }
}
